package demo.string;

public class RemoveKZero {
    public static void main(String[] args) {
        System.out.println(removeKZeros("A00B00C", 2));
        System.out.println(removeKZeros("A00000B000", 3));
        char[] chars = new char[]{'A', 0, 0, 'B', 0, 0, 'C'};
        System.out.println(chars);
        System.out.println(String.valueOf(chars));
    }

    public static String removeKZeros(String str, int k) {
        if (str == null || k < 1) {
            return str;
        }

        char[] chas = str.toCharArray();
        int count = 0, start = -1;
        for (int i = 0; i < chas.length; i++) {
            if (chas[i] == '0') {
                count++;
                if (start == -1) {
                    start = i;
                }
            } else {
                if (count == k) {
                    while (count-- != 0) {
                        chas[start++] = 0;
                    }
                }
                count = 0;
                start = -1;
            }
        }

        if (count == k) {
            while (count-- != 0) {
                chas[start++] = 0;
            }
        }
        return String.valueOf(chas);
    }
}
